//
//  TemplateScreenViewController.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/8/14.
//

#import "TemplateScreenViewController.h"
#import "getPondTissueListModel.h"
#import "AddPondModel.h"
#import "YTBaseSelectStringView.h"
#import "StringArrayScreenShowView.h"

@interface TemplateScreenViewController ()
@property (nonatomic ,strong)  UIScrollView * scrollview ;
@property (nonatomic ,strong) NSMutableArray <templateOptionListModel *>* MBMCArray ;
@property (nonatomic ,strong) NSMutableArray <pondTypeModel *>* TKLXArray ;
@property (nonatomic ,strong) UILabel * MBMCLab;

@end

@implementation TemplateScreenViewController
- (NSMutableArray<templateOptionListModel *> *)MBMCArray{
    if(!_MBMCArray){
        _MBMCArray = [NSMutableArray array];
    }
    return _MBMCArray;
}
- (NSMutableArray<pondTypeModel *> *)TKLXArray{
    if(!_TKLXArray){
        _TKLXArray = [NSMutableArray array];
    }
    return _TKLXArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadNewData];
    self.StatusBarColor = NO;
    UIView * topview = [UIView new];
    topview.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:topview];
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(Knav_HeightX);
    }];
    MJWeakSelf
    UIView * backview = [UIView new].addClickBlock(^{
        [weakSelf popViewController];
    });
    [topview addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(44);
        make.left.bottom.offset(0);
        make.width.offset(kwt(140));
    }];
    
    UIImageView * leftimg = kimageView(@"left_black_path");
    [backview addSubview:leftimg];
    [leftimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(20);
        make.left.offset(kwt(10));
        make.centerY.offset(0);
    }];
    
    
    UILabel * leftlab = [DTKTool labelWithText:@"筛选" andFont:kCFont(19) andTextColor:KcolorHex(@"#333333")];
    [topview addSubview:leftlab];
    [leftlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.centerY.equalTo(backview);
    }];
    
    UIView * line = [UIView new];
    line.backgroundColor = KcolorHex(@"#EFEFEF");
    [topview addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.height.offset(2);
        make.top.equalTo(topview.mas_bottom);
    }];
    
    
    UIView * bottomview = [UIView new];
    bottomview.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:bottomview];
    [bottomview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(kwt(63) + KSafeAreaBottomHeight);
    }];
    UIView * bottomline = [UIView new];
    bottomline.backgroundColor = KcolorHex(@"#EFEFEF");
    [bottomview addSubview:bottomline];
    [bottomline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.height.offset(0.5);
        make.top.offset(0);
    }];
    
    UIButton * qxbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    qxbtn.backgroundColor = KcolorHex(@"#F0F2F4");
    [qxbtn setTitle:@"重置" forState:UIControlStateNormal];
    [qxbtn setTitleColor:KcolorHex(@"#5A6066") forState:UIControlStateNormal];
    qxbtn.titleLabel.font = kCFont(17);
    [DTKTool YJ:kwt(10) andView:qxbtn];
    [qxbtn addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bottomview addSubview:qxbtn];
    [qxbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(kwt(48));
        make.width.offset(kwt(133));
        make.left.offset(kwt(15));
        make.top.offset(kwt(8));
    }];
    
    UIButton * qrbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    qrbtn.backgroundColor = [UIColor gradientColorWithSize:CGSizeMake(kwt(195), 1) direction:GradientColorDirectionLevel startColor:KcolorHex(@"#427FFF") endColor:KcolorHex(@"#3D5FFF")];
    [qrbtn setTitle:@"确定" forState:UIControlStateNormal];
    [qrbtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    qrbtn.titleLabel.font = kCFont(17);
    [DTKTool YJ:kwt(10) andView:qrbtn];
    [qrbtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bottomview addSubview:qrbtn];
    [qrbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(kwt(48));
        make.width.offset(kwt(195));
        make.right.offset(kwt(-15));
        make.top.offset(kwt(8));
    }];
    
    
    
    self.view.backgroundColor = UIColor.whiteColor;
   
    UIScrollView * scrollview = [UIScrollView new];
    self.scrollview = scrollview;
    [self.view addSubview:scrollview];
    [scrollview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.bottom.equalTo(bottomview.mas_top);
        make.top.equalTo(topview.mas_bottom);
    }];
    
    
        
}
- (void)loadNewData{
    [self.view showBusyHUD];
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group); //队列组中加入一个异步操作
    dispatch_group_enter(group); //队列组中加入一个异步操作
    [SCNetManger PostTemplateOptionListWithDict: [NSMutableDictionary dictionary] andCompetionHandlerBlock:^(HTTPArrBase * model) {
        if(model.code ==200){
            NSArray <templateOptionListModel *>* array = [NSArray yy_modelArrayWithClass:templateOptionListModel.class json:model.result ];
            for (templateOptionListModel * mbModel in array) {
                if(mbModel.templateType.intValue == (self.isGR?2:1)){///个人是2公共是1
                    [self.MBMCArray addObject:mbModel];
                }
            }
        } else {
            [self.view showWarning:model.message];
        } 
        dispatch_group_leave(group);

    }];
    [SCNetManger PostPondTypeGetOptionWithDict: [NSMutableDictionary dictionary] andCompetionHandlerBlock:^(HTTPArrBase * model) {

        if(model.code ==200){
            NSArray <pondTypeModel *>* array = [NSArray yy_modelArrayWithClass:pondTypeModel.class json:model.result ];
            
            self.TKLXArray = [NSMutableArray array];
            pondTypeModel * tempmodel = [pondTypeModel new];
            tempmodel.ID = @"****";
            tempmodel.name = @"全部";
            [self.TKLXArray addObject:tempmodel];
            [self.TKLXArray addObjectsFromArray:array];
        } else {
            [self.view showWarning:model.message];
        }
        dispatch_group_leave(group);

    }];
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        [self.view hideBusyHUD];
           [self setupview];
    });
}
- (void)setupview{
    MJWeakSelf
    UILabel * onelab = [DTKTool labelWithText:@"模版名称" andFont:kCFont(17) andTextColor:KcolorHex(@"#242729")];
    [self.scrollview addSubview:onelab];
    [onelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(20));
        make.left.offset(kwt(15));
    }];
    
    UIView * mcView = [UIView new];
    [DTKTool YJ:kwt(5) andView:mcView];
    mcView.backgroundColor = KcolorHex(@"#F2F4F6");
    [self.scrollview addSubview:mcView];
    [mcView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(47));
        make.left.offset(kwt(15));
        make.height.offset(kwt(40));
        make.width.offset(kwt(345));
    }];
    mcView.addClickBlock(^{
        [weakSelf tkmcClick];
    });
    
    NSString * seledString = @"请选择模版名称";
    for (templateOptionListModel * model in self.MBMCArray) {
        if([model.ID isEqualToString:[self.dict objectForKey:@"id"]]){
            seledString = model.name;
        }
    }
    UILabel * MBMCLab = [DTKTool labelWithText:seledString andFont:kFont(16) andTextColor:KcolorHex(@"#242729")];
    self.MBMCLab = MBMCLab;
    [mcView addSubview:MBMCLab];
    [MBMCLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.left.offset(kwt(15));
        make.right.offset(kwt(-60));
    }];
  
    UIImageView * rightimg = kimageView(@"template_enter");
    [mcView addSubview:rightimg];
    [rightimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.width.height.offset(kwt(15));
        make.right.offset(kwt(-12));
    }];
    
    UILabel * twelab = [DTKTool labelWithText:@"池塘类型" andFont:kCFont(17) andTextColor:KcolorHex(@"#242729")];
    [self.scrollview addSubview:twelab];
    [twelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(113));
        make.left.offset(kwt(15));
    }];
    
        
    CGFloat X = kwt(15);
    CGFloat Y = kwt(142);
    NSArray<NSString *>* pondTypearray = [[self.dict objectForKey:@"pondTypeId"] componentsSeparatedByString:@","];
    for (pondTypeModel * model in self.TKLXArray) {
        CGFloat BtnW = [DTKTool textWithtitle:model.name andSize:CGSizeMake(10000, 100) andFout:kCFont(16)].width + kwt(20);
        if(BtnW < kwt(103)){
            BtnW = kwt(103);
        }
        if(BtnW > kwt(345)){
            BtnW = kwt(345);
        }
        gywbtn * btn = [gywbtn buttonWithType:0];
        btn.infoDict = @{@"id":model.ID};
        btn.addTarget(self,@selector(btnClick:));
        btn.addbackgroundColor(KcolorHex(@"#F2F4F6"));
        [DTKTool YJBK:kwt(5) andView:btn andWidth:1 andColor:KcolorHex(@"#DFDFDF")];
        btn.addNormalText(model.name).addNormalColor(KcolorHex(@"#242729")).addFont(kFont(16));
        [btn setTitleColor:KcolorHex(@"#0669FE") forState:UIControlStateSelected];
        [self.scrollview addSubview:btn];
        if(X + BtnW > kwt(360)){
            X = kwt(15);
            Y += kwt(52);
        }
        btn.frame = CGRectMake(X, Y, BtnW, kwt(40));
        X += BtnW+ kwt(18);
        if([pondTypearray containsObject:model.ID]){
            btn.selected = YES;
            btn.addbackgroundColor(KcolorHex(@"#EDF4FF"));
            btn.addFont(kCFont(16));
            [DTKTool YJBK:kwt(5) andView:btn andWidth:1 andColor:KcolorHex(@"#0669FE")];
        }
    }
    self.scrollview.contentSize = CGSizeMake(0, Y + kwt(50));
}
- (void)leftBtnClick{
    [self.dict setObject:@"" forKey:@"id"];
    [self.dict setObject:@"" forKey:@"pondTypeId"];
    [self.scrollview.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self setupview];
}
- (void)rightBtnClick{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)tkmcClick{
    NSString * seledString = @"";
    NSMutableArray * stringArray = [NSMutableArray array];
    for (templateOptionListModel * model in self.MBMCArray) {
        [stringArray addObject:model.name];
        if([model.ID isEqualToString:[self.dict objectForKey:@"id"]]){
            seledString = model.name;
        }
    }
    [StringArrayScreenShowView showviewWithSeledString:seledString andStringArray:stringArray andRightBlock:^(NSString * _Nonnull SeledString) {
        self.MBMCLab.text = SeledString;
        for (templateOptionListModel * model in self.MBMCArray) {
            if([model.name isEqualToString:SeledString]){
                [self.dict setObject:model.ID forKey:@"id"];
            }
        }
    }];
}

- (void)btnClick:(gywbtn *)btn{
    btn.selected = !btn.selected;
    if(btn.selected){
        btn.addbackgroundColor(KcolorHex(@"#EDF4FF"));
        btn.addFont(kCFont(16));
        [DTKTool YJBK:kwt(5) andView:btn andWidth:1 andColor:KcolorHex(@"#0669FE")];
        NSMutableArray<NSString *>* pondTypearray = [[[self.dict objectForKey:@"pondTypeId"] componentsSeparatedByString:@","] mutableCopy];
        [pondTypearray addObject:[btn.infoDict objectForKey:@"id"]];
        if([pondTypearray containsObject:@""]){
            [pondTypearray removeObject:@""];
        }
        [self.dict setObject:[pondTypearray componentsJoinedByString:@","] forKey:@"pondTypeId"];
    } else {
        btn.addbackgroundColor(KcolorHex(@"#F2F4F6"));
        btn.addFont(kFont(16));
        [DTKTool YJBK:kwt(5) andView:btn andWidth:1 andColor:KcolorHex(@"#DFDFDF")];
        NSMutableArray<NSString *>* pondTypearray = [[[self.dict objectForKey:@"pondTypeId"] componentsSeparatedByString:@","] mutableCopy];
        if([pondTypearray containsObject:@""]){
            [pondTypearray removeObject:@""];
        }
        [pondTypearray removeObject:[btn.infoDict objectForKey:@"id"]];
        [self.dict setObject:[pondTypearray componentsJoinedByString:@","] forKey:@"pondTypeId"];
    }
}
@end
